package com.account.book.controller;


import com.account.book.common.context.UserTokenContextHolder;
import com.account.book.common.response.BaseResult;
import com.account.book.common.response.ResultUtils;
import com.account.book.entity.Book;
import com.account.book.request.BookQueryReqVo;
import com.account.book.response.BookResVo;
import com.account.book.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @author liang-hw
 * @className BookController
 * @description
 * @date 2023/11/08 09:57:23
 */
@Api(tags = {"账本接口"})
@RestController
@RequestMapping("/api/book")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * @param @param book
     * @return
     * @author liang-hw
     * @description 创建账本/更新账本
     * @date 2023/11/08 09:58:14
     */
    @ApiOperation(value = "创建账本/更新账本", notes = "创建账本/更新账本")
    @PostMapping("/saveAndUpdate")
    public BaseResult save(@Valid @RequestBody BookQueryReqVo bookQueryReqVo) {
        String id = bookQueryReqVo.getId();
        // 获取用户id
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        // 新建
        if (id == null) {
            bookService.add(bookQueryReqVo);
        } else {
            bookService.update(bookQueryReqVo, userId);
        }
        return ResultUtils.success();
    }

    /**
     * @param
     * @return @return {@link BaseResult }<{@link List }<{@link Book }>>
     * @author liang-hw
     * @description 查询我的账本
     * @date 2023/11/08 16:02:01
     */
    @ApiOperation(value = "查询我的账本", notes = "查询我的账本")
    @GetMapping("/get")
    public BaseResult<List<BookResVo>> get() {
        return ResultUtils.success(bookService.getBook());
    }


    /**
     * @param @param bookId
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 删除账本
     * @date 2023/11/08 15:59:55
     */
    @ApiOperation(value = "删除账本", notes = "删除账本")
    @GetMapping("/delete")
    public BaseResult delete(@RequestParam("bookId") String bookId) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookService.delete(bookId, userId);
        return ResultUtils.success();
    }

    /**
     * @param @param bookId
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 判断当前用户是否是账本成员
     * @date 2023/11/08 16:05:24
     */
    @ApiOperation(value = "判断当前用户是否是账本成员", notes = "判断当前用户是否是账本成员")
    @GetMapping("/isBookMember")
    public BaseResult isBookMember(@RequestParam("bookId") String bookId) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        Long isBookMember = bookService.isBookMember(bookId, userId);
        return ResultUtils.success(isBookMember);
    }

    /**
     * @param @param    bookId
     * @param remark
     * @param timeStamp
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 用户被邀请加入账本
     * @date 2023/11/08 16:04:33
     */
    @ApiOperation(value = "用户被邀请加入账本", notes = "用户被邀请加入账本")
    @GetMapping("/userAddBook")
    public BaseResult userAddBook(@RequestParam("bookId") String bookId, @RequestParam(value = "remark", defaultValue = "", required = false) String remark, @RequestParam(value = "timeStamp") Long timeStamp) throws UnsupportedEncodingException {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        // 单位/分钟
        Long sminute = (System.currentTimeMillis() - timeStamp) / (1000 * 60);
        // 大于一个月，则为失效
        if (sminute.longValue() >= 1 * 60 * 24 * 30) {
            return ResultUtils.error("邀请已失效");
        }
        String decodeRemark = URLDecoder.decode(remark, "UTF-8");
        BookResVo bookResVo = bookService.userAddBook(bookId, decodeRemark,userId);
        return ResultUtils.success(bookResVo);
    }

    /**
     * @param @param bookName
     * @return @return {@link BaseResult }<{@link List }<{@link Book }>>
     * @author liang-hw
     * @description 查询公开账本
     * @date 2023/11/08 16:01:36
     */
    @ApiOperation(value = "查询公开账本", notes = "查询公开账本")
    @GetMapping("/search")
    public BaseResult<List<BookResVo>> searchBook(@RequestParam("bookName") String bookName) throws UnsupportedEncodingException {
        String decode = URLDecoder.decode(bookName, "UTF-8");
        return ResultUtils.success(bookService.searchBook(decode));
    }
}
